package com.gdou.teaching.service.impl;

import com.gdou.teaching.Enum.ClazzStatusEnum;
import com.gdou.teaching.Enum.ResultEnum;
import com.gdou.teaching.Enum.UserStatusEnum;
import com.gdou.teaching.dataobject.Clazz;
import com.gdou.teaching.dataobject.User;
import com.gdou.teaching.exception.TeachingException;
import com.gdou.teaching.mapper.ClazzMapper;
import com.gdou.teaching.mapper.UserMapper;
import com.gdou.teaching.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.Collator;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author carrymaniac
 * @date Created in 14:41 2019-08-14
 * @description
 **/
@Service
@Slf4j
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    ClazzMapper clazzMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public Clazz registerClass(String clazzName) {
        List<Clazz> clazzes = clazzMapper.selectClazzByClassName(clazzName, ClazzStatusEnum.NORMAL.getCode());
        if(clazzes.size()>0){
            throw new TeachingException(ResultEnum.CLASSNAME_ERROR);
        }
        Clazz clazz = new Clazz();
        clazz.setClassName(clazzName);
        clazz.setClassStatus(ClazzStatusEnum.NORMAL.getCode());
        int i = clazzMapper.addClazz(clazz);
        if(i>0){
            return clazz;
        }else {
            throw new TeachingException(ResultEnum.PARAM_ERROR);
        }
    }

    @Override
    public Clazz getClassByClazzId(Integer clazzId) {
        Clazz clazz = clazzMapper.selectClazzByClassId(clazzId,ClazzStatusEnum.NORMAL.getCode());
        if(clazz==null){
            log.error("错误参数为： clazzId:{}",clazzId);
            throw new TeachingException(ResultEnum.CLASS_NOT_EXIST);
        }
        return clazz;
    }

    @Override
    public List<Clazz> getClassesByPage(Integer page, Integer size) {
        List<Clazz> clazzes = clazzMapper.selectClazzByPage(page * size, size, ClazzStatusEnum.NORMAL.getCode());
        if(!(clazzes.size()>0)){
            log.error("分页参数错误: page:{} size:{}",page,size);
            throw new TeachingException(ResultEnum.PARAM_ERROR);
        }
        return clazzes;
    }

    // FIX  增加异常处理
    @Override
    public Boolean updateClazz(Clazz clazz) {
        return clazzMapper.updateClazz(clazz);
    }

    @Override
    public List<Integer> getStudentByClazzId(Integer clazzId) {
        List<User> users = userMapper.selectUserListByClassId(clazzId, UserStatusEnum.NORMAL.getCode());
        return users.stream().map(user -> user.getUserId()).collect(Collectors.toList());
    }

    @Override
    public Integer getStudentCountByClazzId(Integer clazzId){
        Integer num = userMapper.getCountByClassId(clazzId, UserStatusEnum.NORMAL.getCode());
        return num;
    }


    @Override
    public List<Map.Entry<Integer, String>> getAllClazzList(){
        List<Clazz> clazzes = clazzMapper.selectClazzList(ClazzStatusEnum.NORMAL.getCode());
        Map<Integer,String> map = new TreeMap<>();
        clazzes.forEach(clazze -> {
            map.put(clazze.getClassId(),clazze.getClassName());
        });
        List<Map.Entry<Integer, String>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, (o1, o2) -> Collator.getInstance(Locale.CHINESE).compare(o1.getValue(),o2.getValue()));
        return list;
    }


}
